<?php
namespace app\home\controller;

use app\common\model\NewsCommentLike as NewsCommentLikeModel;
use app\common\model\NewsCommentMessage as NewsCommentMessageModel;
use app\common\model\NewsComment as NewsCommentModel;

/**
 * 资讯评论点赞控制器
 * News Newscommentlike
 * @package app\index\controller
 */
class Newscommentlike extends Base
{
    /**
     * [add  添加]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new NewsCommentLikeModel;
        $cmodel = new NewsCommentModel;
        $nmodel = new NewsCommentMessageModel;
        $user = $this->user;
        try{
            $like = $model->findOne(['cid'=>$param['cid'],'mid'=>$user['id']]);
            if(empty($like)){
                $param['mid'] = $user['id'];
                $id = $model->addData($param);
                $comment = $cmodel->findData($param['cid'],'id','mid,comment_uid');
                if(empty($comment['comment_uid'])){
                    $mid = $comment['mid'];
                }else{
                    $mid = $comment['comment_uid'];
                }
                $nmodel->addData(['mid'=>$mid,'cid'=>$id,'type'=>2]);
                add_quality($user['id'],29,$user['quality'],4);
            }elseif($like['status'] == 1){
                $param['status'] = 2;
                $param['id'] = $like['id'];
                $model->editData($param);
            }else{
                $param['status'] = 1;
                $param['id'] = $like['id'];
                $model->editData($param);
                add_quality($user['id'],29,$user['quality'],4);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, '点赞失败'.$e->getMessage());
        }
        return admin_json(1, '点赞成功');
    }
}